package com.europe.business.europebusiness.ui.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SpUtils:使用SharedPreferences 存储方式存储数据到本地
 * 参数   context   key   value
 */
public class SpUtils {
    public static boolean saveToSp(Context context, String key, Object value){
        boolean isSaved = true;
        SharedPreferences sp = context.getSharedPreferences("eb", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if(value instanceof String){
            editor.putString(key, value.toString());
        }else if(value instanceof Float){
            editor.putFloat(key, (Float) value);
        }else if(value instanceof Boolean){
            editor.putBoolean(key, (Boolean)value);
        }else if(value instanceof Integer){
            editor.putInt(key, (Integer) value);
        }else if(value instanceof Long){
            editor.putLong(key, (Long) value);
        }else{
            isSaved = false;
        }
        if(isSaved){
            editor.commit();
        }
        return isSaved;

    }

    public static String getSpStringValue(Context context, String key){

        SharedPreferences sp = context.getSharedPreferences("eb", Context.MODE_PRIVATE);
        return  sp.getString(key, null);
    }
}
